#include "dbl.h"

dplist* dplistcreat(void)//创建双链表
{
    dplist* head;
    head = (dplist*)malloc(sizeof(dplist));
    if(head == NULL)
    {
        printf("%s 创建失败!\n",__func__);
        return NULL;
    }
    head->data = 0;
    head->pre = NULL;
    head->next = NULL;

    return head;
}

int dplistshow(dplist* head )//遍历函数
{
    if(head == NULL)
    {
        printf("链表为空!\n");
        return -1;
    }
     printf("从头向尾遍历\n");
    while(head->next!=NULL)
    {
        printf("-%d",head->next->data);
        head = head->next;
    }
    putchar(10);
    /* printf("从尾向头遍历\n");
    while(head->pre!=NULL)
    {
        printf("-%d",head->data);
        head = head->pre;
    }
      putchar(10); */
    return 0;

}

int dplisthead(dplist* head, datatype data)//头插入
{
    dplist* temp;
    temp = (dplist*)malloc(sizeof(dplist));
    if(temp == NULL)
    {
        printf("内存分配失败!!\n");
        return -1;
    }

    temp->data = data;

    temp->next = head->next;
    temp->pre = head;
    if(head->next!=NULL)
    {
        temp->next->pre = temp;
    }
    head->next = temp;

    return 0;
}

int dplisttail(dplist* head, datatype data) //尾插入
{
     if(head == NULL)
    {
        printf("链表为空!\n");
        return -1;
    }
    dplist* temp;
    temp = (dplist*)malloc(sizeof(dplist));
    if(temp == NULL)
    {
        printf("内存分配失败!!\n");
        return -1;
    }
    while (head->next!=NULL)
    {
       head = head->next;
    }

    temp->data = data;

    head->next = temp;
    temp->pre = head;
    temp->next = NULL;

    return 0;
}

int dblinsertpos(dplist*head,datatype data, int pos) //按位置插入
{
    dplist* temp;
    temp = (dplist*)malloc(sizeof(dplist));
    if(temp == NULL)
    {
        printf("内存分配失败!!\n");
        return -1;
    }
    if(pos<0)
    {
        printf("插入位置有误!!\n");
        return -1;

    }
    while(head)
    {
        if(pos!=0)
        {
            head = head->next;
            pos--;
        }
        else
        {
            temp->data = data;

            temp->next = head->next;
            temp->pre = head;
            if(head->next != NULL)
            {
                temp->next->pre = temp;
            }
            head->next = temp;
            return 0;
        }
    }
        printf("插入位置有误!!\n");
        return -1;

}

datatype  dpldeletehead(dplist* head)  //头删除
{
    if(head == NULL)
    {
        printf("链表为空!\n");
        return (datatype)-1;
    }

    dplist* temp;
    temp = head->next;
    datatype datasave = temp->data;
    if(temp->next!=NULL)
    temp->next->pre = head;
    head->next = temp->next;

    if (temp != NULL)
    {
        free(temp);
        temp = NULL;
    }
       return datasave;
}

datatype dpldeletetail(dplist* head) //尾删除
{
    if(head == NULL)
    {
        printf("链表为空!\n");
        return (datatype)-1;
    }
    while(head->next->next != NULL)
    {
        head = head->next;
    }
    dplist* temp;
    temp = head->next;
    datatype datasave = temp->data;
    
    head->next = temp->next;

    if (temp != NULL)
    {
        free(temp);
        temp = NULL;
    }
       return datasave;
}

int dbldeletepos(dplist*head, int pos) //按位置删除
{
    dplist* temp;
    temp = (dplist*)malloc(sizeof(dplist));
    if(temp == NULL)
    {
        printf("内存分配失败!!\n");
        return -1;
    }
    if(pos<0)
    {
        printf("插入位置有误!!\n");
        return -1;

    }
    while(head->next)
    {
        if(pos!=0)
        {
            head = head->next;
            pos--;
        }
        else
        {
            dplist* temp;
            temp = head->next;
            datatype datasave = temp->data;
            if(temp->next!=NULL)
            temp->next->pre = head;
            head->next = temp->next;

        if (temp != NULL)
        {
            free(temp);
            temp = NULL;
        }
            return datasave; 
        }
    }
        printf("插入位置有误!!\n");
        return -1;

}


int reseverlist(dplist* head)//逆序
{
     if(head == NULL)
    {
        printf("链表为空!\n");
        return -1;
    }
    dplist* temp;
    dplist* s;
    temp = head->next;
    while(1)
    {
        s= temp->next;
        temp->next = temp->pre;
        temp->pre = s;
        if(temp->pre == NULL){
        break;}
        else{
        temp = temp->pre;
        }
    }
    head->next->next = NULL;
    head->next = temp;
    temp->pre = head;
    return 0;

}